package com.example.print.other;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.print.R;

public class LanguageAdapter extends BaseAdapter {
    private Context context;
    private String[] languages;
    private int selectedPosition;

    public LanguageAdapter(Context context, String[] languages, int selectedPosition) {
        this.context = context;
        this.languages = languages;
        this.selectedPosition = selectedPosition;
    }

    @Override
    public int getCount() {
        return languages.length;
    }

    @Override
    public Object getItem(int position) {
        return languages[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_language, parent, false);
        }

        TextView textView = convertView.findViewById(R.id.tv_language);
        textView.setText(languages[position]);

        // 根据选中状态设置背景颜色
        if (position == selectedPosition) {
            textView.setBackgroundColor(context.getResources().getColor(R.color.lightskyblue));
        } else {
            textView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
        }

        return convertView;
    }

    public void setSelectedPosition(int position) {
        this.selectedPosition = position;
        notifyDataSetChanged();
    }
}
